package com.wedata.wegymer;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class 调健萌接口 {

    /*private static String uri = "https://api3t.gymcity.com.cn";
    private static String appId = "q0fkixdss9rjesui";
    private static String secretKey = "c8pk93j6qfio9uitzplc2pt5qaql547g";*/
    private static String uri = "https://api3.gymcity.com.cn";
    private static String appId = "58376284769";
    private static String secretKey = "aGJMc7syzcPJ5ZyxYKPxfySNZw26ccJz";

    public static void main(String[] args) {
        //String string = "1078198171832020834,pay_success;1085946013237143572,verify;1086273814159064278,verify";
        String string = "1078496365274906631,verify;1085843071650264039,verify;1086020808343064146,verify";
        String[] str = string.split(";");
        for(int i = 0; i < str.length; i++){
            String[] ss = str[i].split(",");
            douYinCallBack(ss[0],ss[1]);
        }
    }

    public static void douYinCallBack(String orderId,String action) {
        JSONObject data = new JSONObject();
        data.put("order_id",orderId);
        data.put("action",action);
        JSONObject obj = HttpUtils.sendPostByForm(uri+"/openapi/callBack/douYinCallBack", putSign(data));
        System.out.println(obj);
    }

    /**
     * 健萌的加签
     * @param data
     * @return
     */
    private static String putSign(JSONObject data){
        data.put("appid",appId);
        data.put("timestamp", System.currentTimeMillis()/1000);

        StringBuffer postData = new StringBuffer();
        postData.append("data=").append(data);
        postData.append("&sign=").append(genSign(data));

        return postData.toString();
    }

    /**
     * 健萌的签名算法
     * @param body
     * @return
     */
    private static String genSign(JSONObject body){
        StringBuffer signData = new StringBuffer(secretKey);

        Set<String> set = body.keySet();
        List<String> list = new ArrayList();
        for(String key : set){
            list.add(key);
        }
        Collections.sort(list);
        for(String key : list){
            Object val = body.get(key);
            if(val == null){
                continue;
            }
            signData.append(key);
            if(val instanceof Boolean){
                signData.append((Boolean)val?1:0);
            }else if(val instanceof List){
                List ll = (List)val;
                for(int i=0; i<ll.size(); i++){
                    //这里只能是数字或字符串不能是对象，如果是对象，需要再进入一层循环，暂时用不到，就不写了
                    signData.append(i).append(ll.get(i));
                }
            }else{
                signData.append(val);
            }

        }
        signData.append(secretKey);

        String sign = StringUtils.upperCase(MD5.create().digestHex(signData.toString()));
        return sign;
    }


}
